<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: DownloadPortlet.php 401 2011-01-04 21:18:30Z steen.rabol $
 *
 */

Prado::using('Application.Portlets.Portlet');
class DownloadPortlet extends Portlet
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->bindData();
	}

	public function bindData()
	{
		$grps = $this->Page->DataAccess->getAll("select * from tbldownloadportletgrps where active=1");
		$this->MyDataList->DataSource = $grps;
		$this->MyDataList->dataBind();
	}

	public function getDownloadGroupsData()
	{
		return $this->Page->DataAccess->getAll("select * from tbldownloadportletgrps order by grpname");
	}

	public function onItemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "ShowContent":
				$grpid = $p->CommandParameter;
				$this->FilesDataList->DataSource=$this->Page->DataAccess->getAll("select * from tbldownloadfiles where active=1 && grpid=$grpid");
				$this->FilesDataList->dataBind();
				break;

			case "SendFile":
				$fileid		= $p->CommandParameter;
				$db			= $this->Application->getModule('db');
				$savepath	= dirname($this->getRequest()->getApplicationFilePath()). '/' . "protected/Storage/DownloadPortlet/";
				$file		= $db->getOne("select * from tbldownloadfiles where id=$fileid");
				$filename	= $savepath . $file['filename'];

				$this->Response->writeFile($filename);
				$sql = "update tbldownloadfiles set dlcount=dlcount+1 where id=$fileid";
				$db->Execute($sql);
				$this->FilesDataList->DataSource=$this->Page->DataAccess->getAll("select * from tbldownloadfiles where active=1 && grpid=" . $file['grpid']);
				$this->FilesDataList->dataBind();
				break;
		}
	}
}
?>